package tv.huan.web.controller.media;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.framework.util.ShiroUtils;
import tv.huan.media.domain.Album;
import tv.huan.media.domain.Authorizer;
import tv.huan.media.domain.Classification;
import tv.huan.media.service.IAlbumService;
import tv.huan.media.service.IAuthorizerService;
import tv.huan.media.service.IClassificationService;
import tv.huan.system.domain.SysUser;

import java.util.List;

/**
 * 专辑Controller
 * 
 * @author admin
 * @date 2019-10-08
 */
@Controller
@RequestMapping("/media/album")
public class AlbumController extends BaseController
{
    private String prefix = "media/album";

    @Autowired
    private IAlbumService albumService;

    @Autowired
    private IClassificationService classificationService;

    @Autowired
    private IAuthorizerService authorizerService;

    @RequiresPermissions("media:album:view")
    @GetMapping()
    public String album(ModelMap mmap)
    {
        mmap.put("classificationList", classificationService.selectClassificationListAll());
        mmap.put("classification", new Classification());
        mmap.put("authorizerList", authorizerService.selectAuthorizerListAll());
        mmap.put("authorizer", new Authorizer());
        mmap.put("businessLeaderList", authorizerService.selectBusinessLeader());
        return prefix + "/album";
    }

    /**
     * 查询专辑列表
     */
    @RequiresPermissions("media:album:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Album album)
    {
        startPage();
        List<Album> list = albumService.selectAlbumList(album);
        return getDataTable(list);
    }

    /**
     * 下载模板
     */
    @RequiresPermissions("media:album:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Album> util = new ExcelUtil<Album>(Album.class);
        return util.importTemplateExcel("专辑导入模板");
    }

    /**
     * 导入专辑
     */
    @Log(title = "专辑管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("media:album:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Album> util = new ExcelUtil<Album>(Album.class);
        List<Album> albumList = util.importExcel(file.getInputStream());
        String message = albumService.importAlbum(albumList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导出专辑列表
     */
    @RequiresPermissions("media:album:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Album album)
    {
        List<Album> list = albumService.selectAlbumList(album);
        ExcelUtil<Album> util = new ExcelUtil<Album>(Album.class);
        return util.exportExcel(list, "album");
    }

    /**
     * 新增专辑
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("classificationList", classificationService.selectClassificationListAll());
        mmap.put("classification", new Classification());
        mmap.put("authorizerList", authorizerService.selectAuthorizerListAll());
        mmap.put("authorizer", new Authorizer());
        mmap.put("businessLeaderList", authorizerService.selectBusinessLeader());
        return prefix + "/add";
    }

    /**
     * 新增保存专辑
     */
    @RequiresPermissions("media:album:add")
    @Log(title = "专辑", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Album album)
    {
        return toAjax(albumService.insertAlbum(album));
    }

    /**
     * 修改专辑
     */
    @GetMapping("/edit/{albumId}")
    public String edit(@PathVariable("albumId") Long albumId, ModelMap mmap)
    {
        Album album = albumService.selectAlbumById(albumId);
        mmap.put("album", album);
        mmap.put("classificationList", classificationService.selectClassificationListAll());
        mmap.put("authorizerList", authorizerService.selectAuthorizerListAll());
        mmap.put("businessLeaderList", authorizerService.selectBusinessLeader());
        return prefix + "/edit";
    }

    /**
     * 修改保存专辑
     */
    @RequiresPermissions("media:album:edit")
    @Log(title = "专辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Album album)
    {
        return toAjax(albumService.updateAlbum(album));
    }

    /**
     * 删除专辑
     */
    @RequiresPermissions("media:album:remove")
    @Log(title = "专辑", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            albumService.deleteAlbumByIds(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 推荐
     */
    @RequiresPermissions("media:album:recommend")
    @GetMapping("/recommend/{albumIds}")
    public String recommend(@PathVariable("albumIds") String albumIds, ModelMap mmap)
    {
        mmap.put("albumIds", albumIds);
        return prefix + "/recommend";
    }

    /**
     * 推荐（保存）
     */
    @RequiresPermissions("media:album:list")
    @PostMapping("/recommend")
    @ResponseBody
    public AjaxResult recommend(String albumIds, String itemIds)
    {
        //当前用户
        SysUser user = ShiroUtils.getSysUser();
        try {
            this.albumService.recommend(albumIds, itemIds, user);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("推荐成功");
    }

    /**
     * 下线通知
     */
    @RequiresPermissions("media:album:offLineNotice")
    @Log(title = "下线通知", businessType = BusinessType.OFFLINENOTICE)
    @PostMapping( "/offLineNotice")
    @ResponseBody
    public AjaxResult offLineNotice(String ids, String offLineReason)
    {
        try {
            albumService.offLineNotice(ids,offLineReason);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 下线原因
     */
    @GetMapping("/offLineNotice/{ids}")
    public String operateReason(@PathVariable("ids") String ids, ModelMap mmap)
    {
        mmap.put("ids", ids);
        return prefix + "/offLineNotice";
    }
}
